<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Records
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: LineStyle.php 6 2010-02-17 04:28:26Z indigo.sunrise $
 */

/**
 * LineStyle record.
 * 
 * @package    Pwiff
 * @subpackage Records
 */
class Pwiff_Record_LineStyle extends Pwiff_Record
{
	/**
	 * DefineShape version this record is being used with.
	 * 
	 * @var int
	 */
	protected $shapeVersion = 1;
	
	/**
	 * Color of the line. RGB or RGBA (Shape3+).
	 * 
	 * @var Pwiff_Record_RGB
	 */
	protected $color = null;
	
	/**
	 * Line width in twips.
	 * 
	 * @var int
	 */
	protected $width = 20;
	
	/**
	 * Sets the DefineShape version.
	 * 
	 * @param  int $version
	 * @return Pwiff_Record_LineStyle
	 */
	public function setShapeVersion($version)
	{
		$this->shapeVersion = $version;
		return $this;
	}
	
	/**
	 * Sets the RGB or RGBA line color.
	 * 
	 * @param  Pwiff_Record_RGB $color
	 * @return Pwiff_Record_LineStyle
	 */
	public function setColor(Pwiff_Record_RGB $color)
	{
		if ($this->shapeVersion < 3 AND ($color instanceof Pwiff_Record_RGBA)) {
			throw new Pwiff_Exception('At least DefineShape3 is required for RGBA color.');
		}
		
		if ($this->shapeVersion >= 3 AND !($color instanceof Pwiff_Record_RGBA)) {
			throw new Pwiff_Exception('An RGBA color is required for DefineShape3 and higher.');
		}
		
		$this->color = $color;
		return $this;
	}
	
	/**
	 * Sets the line width in twips.
	 * 
	 * @param  int $width
	 * @return Pwiff_Record_LineStyle
	 */
	public function setWidth($width)
	{
		$this->width = $width;
	}
	
	/**
	 * Gets the DefineShape version.
	 * 
	 * @return int
	 */
	public function getShapeVersion()
	{
		return $this->shapeVersion;
	}
	
	/**
	 * Gets the line color.
	 * 
	 * @return Pwiff_Record_RGB
	 */
	public function getColor()
	{
		return $this->color;
	}
	
	/**
	 * Gets the line width.
	 * 
	 * @return int
	 */
	public function getWidth()
	{
		return $this->width;
	}
	
	/**
	 * Gets the record length.
	 * 
	 * @return int
	 */
	public function getRecordLength()
	{
		// width + color
		$length = 2 + $this->color->getRecordLength();
		return $length;
	}
	
	/**
	 * Writes the LineStyle record to the given output instance.
	 * 
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		$out->writeUI16($this->width);
		$this->color->write($out);
	}
}
